import { find, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
import { view } from 'cc';
import { UITransform } from 'cc';
import { _decorator, Component, Label, Node } from 'cc';
import { App } from '../../../App';
import AbstractComponent from '../../../base/AbstractComponent';
import { EventConst } from '../../../common/Common';
import { MultiCommon } from '../../../hall/multiplay/MultiCommon';
import { showLuckyBallRule, showTipBox } from '../../../tools/PopuUp';
import UtilTool from '../../../tools/UtilTool';
import { GameModel } from '../model/GameModel';
import { GameLayer5 } from './GameLayer5';
import { GameModel5 } from './GameModel5';
const { ccclass, property } = _decorator;

// 结算界面
@ccclass('EndLayer5')
export class EndLayer5 extends Component {
    @property(Node)
    successNode : Node = null

    @property(Node)
    failNode : Node = null


    private viewState = null

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())

        

    }

    /** 展示界面
     * @param state [1]胜利界面 [2]失败界面
     */
    showView(data, state) {
        this.viewState = state
        UtilTool.nodeActive(state==1?this.successNode:this.failNode,5,0.16)

        this.successNode.active = state==1
        this.failNode.active = state==2

        // 界面赋值
        let parentNode = state==1?this.successNode:this.failNode
        let box1 = find("myNode/box",parentNode)
        let address1 = find("myNode/address",parentNode).getComponent(Label)
        let winnerLabel1 = find("myNode/winnerLabel",parentNode)

        let box2 = find("oherNode/box",parentNode)
        let address2 = find("oherNode/address",parentNode).getComponent(Label)
        let winnerLabel2 = find("oherNode/winnerLabel",parentNode)

        let coinLabel = find("myNode/coinLabel",parentNode).getComponent(Label)
        let coin = find("myNode/coin",parentNode)

        // 头像
        let head1 = find("myNode/head",parentNode)
        let head2 = find("oherNode/head",parentNode)

        box1.active = state==1
        box2.active = state==2
        winnerLabel1.active = state==1
        winnerLabel2.active = state==2

        if (state==1) { // 胜利
            App.Audio.playOneShot("game/win")
            address1.string = UtilTool.formatNickOrAddress({"nickname":data.winPlayerNickname, "address":data.winPlayerAccount, "addressLen":6})
            address2.string = UtilTool.formatNickOrAddress({"nickname":data.losePlayerNickname, "address":data.losePlayerAccount, "addressLen":6})

            coinLabel.string = "+" + data.reward
            UtilTool.adaptiveLabel([coinLabel.node, coin], 0, 6)

            // 头像
            this.updateHead(head1, App.Model.gameManage.gameStartBloodData.headIdMap[data.winPlayerAccount])
            this.updateHead(head2, App.Model.gameManage.gameStartBloodData.headIdMap[data.losePlayerAccount])

            // 星币数
            let starCoinLabel = find("myNode/starCoinLabel",parentNode).getComponent(Label)
            let starCoin = find("myNode/starCoin",parentNode)
            starCoinLabel.string = "+" + data.winExperience
            UtilTool.adaptiveLabel([starCoinLabel.node, starCoin], 0, 5)
        }
        else if (state==2) { // 失败
            App.Audio.playOneShot("game/fail")
            address2.string = UtilTool.formatNickOrAddress({"nickname":data.winPlayerNickname, "address":data.winPlayerAccount, "addressLen":6})
            address1.string = UtilTool.formatNickOrAddress({"nickname":data.losePlayerNickname, "address":data.losePlayerAccount, "addressLen":6})

            coinLabel.string = "-" + data.reward
            UtilTool.adaptiveLabel([coinLabel.node, coin], 0, 6)

            // 头像
            this.updateHead(head1, App.Model.gameManage.gameStartBloodData.headIdMap[data.losePlayerAccount])
            this.updateHead(head2, App.Model.gameManage.gameStartBloodData.headIdMap[data.winPlayerAccount])

            // 星币数
            let starCoinLabel = find("myNode/starCoinLabel",parentNode).getComponent(Label)
            let starCoin = find("myNode/starCoin",parentNode)
            starCoinLabel.string = "+" + data.loseExperience
            UtilTool.adaptiveLabel([starCoinLabel.node, starCoin], 0, 5)
        }
        else {
            this.node.destroy()
            return
        }

        this.playAnimation(state)
    }

    // 更新头像
    updateHead(headNode, headId) {
        let headDict = App.Scene.getHallRes("images/head")
        if (headDict["head_" + headId]) {
            headNode.getComponent(Sprite).spriteFrame = headDict["head_" + headId]
        }
    }


    // 做动画
    playAnimation(state){
        if (state==1) { // 胜利
            // 动画
            let title = find("title",this.successNode)
            let img_hua1 = find("title/img_hua1",this.successNode)
            let img_hua2 = find("title/img_hua2",this.successNode)
            title.setScale(0.01, 0.01, 1)
            img_hua1.setScale(0.01, 0.01, 1)
            img_hua2.setScale(0.01, 0.01, 1)
            tween(title)
                .to(0.2,{scale: new Vec3(1.4, 1.4, 1)})
                .to(0.2,{scale: new Vec3(0.8, 0.8, 1)})
                .to(0.1,{scale: new Vec3(1.0, 1.0, 1)})
                .start()

            tween(img_hua1)
                .delay(0.5)
                .to(0.15,{scale: new Vec3(1.4, 1.4, 1)})
                .to(0.1,{scale: new Vec3(0.9, 0.9, 1)})
                .to(0.1,{scale: new Vec3(1.0, 1.0, 1)})
                .start()

            tween(img_hua2)
                .delay(0.5)
                .to(0.15,{scale: new Vec3(1.4, 1.4, 1)})
                .to(0.1,{scale: new Vec3(0.9, 0.9, 1)})
                .to(0.1,{scale: new Vec3(1.0, 1.0, 1)})
                .start()
        }
        else if (state==2) { // 失败
            for (let index = 1; index <= 4; index++) {
                let img_yuan = find("title/img_yuan" + index, this.failNode)
                img_yuan.active = false

                let img2 = find("title/img2",this.failNode)
                let img3 = find("title/img3",this.failNode)
                img2.setScale(0.01, 0.01, 1)
                img3.setScale(0.01, 0.01, 1)
                tween(img2)
                    .to(0.2,{scale: new Vec3(1.4, 1.4, 1)})
                    .to(0.2,{scale: new Vec3(0.8, 0.8, 1)})
                    .to(0.1,{scale: new Vec3(1.0, 1.0, 1)})
                    .start()

                tween(img3)
                    .to(0.2,{scale: new Vec3(1.4, 1.4, 1)})
                    .to(0.2,{scale: new Vec3(0.8, 0.8, 1)})
                    .to(0.1,{scale: new Vec3(1.0, 1.0, 1)})
                    .start()
                
                setTimeout(() => {
                    if (this && this.node) {
                        img_yuan.active = true
                        tween(img_yuan)
                            .by(1.5, { angle: -360 })
                            .repeat(3)
                            .start()
                    }
                }, 500)
            }
        }
    }

    /** 
     * 重新匹配
      */
    clickAgain() {
        App.Audio.playOneShot("click")
        this.node.destroy();
        let gameView = App.Model.gameView as GameModel5
        gameView.gameLogic.exitGame()
        App.Model.gameView.backToBloodSelect(App.Model.gameManage.gameStartBloodData.bloodLevel)
    }

    /** 
     * 返回
      */
    clickBack() {
        App.Audio.playOneShot("click")

        this.node.destroy()
        let gameView = App.Model.gameView as GameModel5
        gameView.gameLogic.exitGame()
        App.Model.gameView.backToBloodSelect()
    }

    onDestroy() {
        
    }

}

